// pages/home.js
import { Home } from '../index/home-model.js';
var home = new Home();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loadingHidden: false,
    currentTabsIndex: 0,
    selected: true,
    selected1: false,
    selectedall: false,
    selected2: true,
    selected3: false,
    searchKey: '',
  },

  selected: function (e) {
    this.setData({
      selected1: false,
      selected: true
    })
  },
  selected1: function (e) {
    this.setData({
      selected: false,
      selected1: true
    })
  },
  selectedall: function (e) {
    this.setData({
      selected2: false,
      selected3: false,
      selectedall: true
    })
  },
  selected2: function (e) {
    this.setData({
      selectedall: false,
      selected3: false,
      selected2: true
    })
  },
  selected3: function (e) {
    this.setData({
      selected2: false,
      selectedall: false,
      selected3: true
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._loadData();
  },

  onShow: function (options) {
    this._loadData();
  },

  _loadData: function () {
    var that = this;

    home.getNews((res) => {
      that.setData({
        newsData: res
      });
    })

    home.getPetsData((res) => {
      that.setData({
        allData: res.all,
        catData: res.cat,
        dogData: res.dog,
        loadingHidden: true
      });
    })

    home.getDiaryData((res) => {
      console.log(res)
      that.setData({
        diaryData: res,
      });
    })

    home.getRankData((res) => {
      that.setData({
        feedRankData: res.feed,
        collectRankData: res.collect,
      });
    })

    home.getAd((res) => {
      that.setData({
        adData: res
      })
    })
  },

  diaryTap: function (e) {
    var id = home.getDataSet(e, 'id');
    wx.navigateTo({
      url: '../diary/diary?id=' + id,
    })
  },

  // 跳转到宠物详情页
  onPetsInfo: function (e) {
    var id = home.getDataSet(e, 'id');
    wx.navigateTo({
      url: '../pets-info/pets-info?id=' + id,
    })
  },

  randChange: function (e) {
    var that = this;
    var typeId = home.getDataSet(e, 'type');
    home.randChange(typeId, (res) => {
      if (typeId == 3) {
        that.setData({
          allData: res,
        })
      } else if (typeId == 0) {
        that.setData({
          catData: res,
        })
      } else if (typeId == 1) {
        that.setData({
          dogData: res,
        })
      }
    })
  },

  advertTap: function (e) {
    var id = home.getDataSet(e, 'id');
    wx.navigateTo({
      url: '../advert/advert?id=' + id,
    })
  },

  onPaihangbang: function (e) {
    var id = home.getDataSet(e, 'id');
    wx.navigateTo({
      url: '../paihangbang/paihangbang',
    })
  },

  diaryTap: function (e) {
    var id = home.getDataSet(e, 'id');
    wx.navigateTo({
      url: '../diary/diary?id=' + id,
    })
  },

  onNews: function (e) {
    var id = home.getDataSet(e, 'id');
    wx.navigateTo({
      url: '../news-info/news-info?id=' + id,
    })
  },

  onNewsList: function(e) {
    wx.navigateTo({
      url: '../news/news',
    })
  },

  searchSubmit: function (e) {
    wx.navigateTo({
      url: '../search/search?key=' + this.data.searchKey,
    })
  },

  searchInputEvent: function (e) {
    this.setData({
      searchKey: e.detail.value
    })
  },

  /*下拉刷新页面*/
  onPullDownRefresh: function () {
    this._loadData(() => {
      wx.stopPullDownRefresh()
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    // return {
    //   title: '微宠相册',
    //   path: 'pages/home/home'
    // }
  }
})